﻿using CPPEI.BD.VideoMatrixTool.Datas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.BD.VideoMatrixTool
{
  
    public class XSolutionConfigUntil
    {
        private static List<Solution> ListSolution;

        /// <summary>
        ///  根据方案名称获取对应的视频源和视频矩阵的对应关系
        /// </summary>
        /// <param name="name">方案名称</param>
        /// <param name="configFile">配置文件的路径</param>
        /// <returns></returns>
        public static List<Solution> GetListSolutionByName(string name, string configFile)
        {
            if (System.IO.File.Exists(configFile))
            {
                XmlDocument xmlDocument = new XmlDocument();
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                XmlReader reader = XmlReader.Create(configFile, settings);
                xmlDocument.Load(reader);

                string rootName = "Solution-List";

                XmlNode rootNode = null;
                ListSolution = new List<Solution>();
                try
                {
                    ////根据xClassConfig的Name获取根节点
                    rootNode = xmlDocument.SelectSingleNode("./" + rootName);

                    if (rootNode == null || rootNode.ChildNodes == null)
                    {
                        return null;
                    }

                    foreach (XmlNode childNode in rootNode.ChildNodes)
                    {
                        XmlElement xe = (XmlElement)childNode;
                        if (name.Equals(xe.GetAttribute("Name").ToString()))
                        {
                            Solution solution = new Solution();
                            solution.Name = xe.GetAttribute("Name").ToString();
                            solution.Caption = xe.GetAttribute("Caption").ToString();
                            // 得到solution节点的所有子节点
                            XmlNodeList childNodeList = xe.ChildNodes;
                            solution.Mapping = new List<Mapping>();

                            foreach (XmlNode node in childNodeList)
                            {
                                Mapping mapping = new Mapping();
                                XmlElement element = (XmlElement)node;
                                mapping.VideoMatrixId = element.GetAttribute("VideoMatrixId");
                                mapping.VideoSourceId = element.GetAttribute("VideoSourceId");
                                solution.Mapping.Add(mapping);
                            }
                            ListSolution.Add(solution);
                        }

                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    reader.Close();
                }

            }
            return ListSolution;
        }
    }
}
